//
//  Stack.hpp
//  Queue
//
//  Created by chenyh on 2021/2/5.
//

#ifndef Stack_hpp
#define Stack_hpp
#include <iostream>
using namespace std;
template <typename DataType>
class Stack {
public:
    Stack(int size)
    {
        maxSize = size;
        top = -1;
        elements = new DataType[size];
    }
    ~Stack()
    {
        delete [] elements;
    }
    //入栈操作
    bool push(DataType data);
    //出栈操作
    DataType pop();
    //判空
    bool isEmpty();
    //获取元素数
    int eleNumber();
private:
    DataType *elements;
    int         top;
    int         maxSize;
};

template <typename DataType>
bool Stack<DataType>:: push(DataType data) {
    if (top == maxSize) {
        return false;  //栈满
    }
    elements[++top] = data;
    return true;
}


template <typename DataType>
DataType Stack<DataType>:: pop() {
    if (top == -1) {
        exit(1);
    }
    return elements[top--]; //返回栈顶元素值
}

template <typename DataType>
bool Stack<DataType>:: isEmpty() {
    if (top == -1) {
        return true;
    }
    return false; //返回栈顶元素值
}

template <typename DataType>
int Stack<DataType>:: eleNumber() {
    if (top >= 0) {
        return top+1;
    }
    return 0; //返回栈顶元素值
}


#endif /* Stack_hpp */
